package com.ycsj.goldmedalnewconcept.parent.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.aakira.expandablelayout.ExpandableLayout;
import com.github.aakira.expandablelayout.ExpandableLayoutListenerAdapter;
import com.github.aakira.expandablelayout.ExpandableRelativeLayout;
import com.github.aakira.expandablelayout.Utils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.util.MathUtil;
import com.ycsj.goldmedalnewconcept.parent.bean.MsgInfo;

import java.text.NumberFormat;
import java.util.List;

/**
 * 班级信息详情的列表适配器
 * Created by Administrator on 2018/7/17 0017.
 */

public class ClassMsgRvAdapter extends BaseQuickAdapter<MsgInfo, BaseViewHolder> {

    private List<MsgInfo> datas;

    private SparseBooleanArray expandState;

    private Context context;

    //班级对应的老师账号
    private String c;

    public ClassMsgRvAdapter(Context context, String c, int layoutResId, @Nullable List<MsgInfo> msgInfos) {
        super(layoutResId, msgInfos);
        this.context = context;
        this.datas = msgInfos;
        this.c = c;

        expandState = new SparseBooleanArray();

        //设置默认的展开位置,默认第1个条目为展开位置s
        for (int i = 0; i < datas.size(); i++) {
            if (i == 0) {
                expandState.put(0, true);
            } else {
                expandState.put(i, false);
            }
        }
    }

    @Override
    protected void convert(final BaseViewHolder helper, MsgInfo item) {

        //折叠布局
        final ExpandableRelativeLayout expandableLayout = helper.getView(R.id.expandableLayout);
        expandableLayout.setInterpolator(item.interpolator);
        expandableLayout.setExpanded(expandState.get(helper.getLayoutPosition()));

        //班级名称
        final TextView tvClassName = helper.getView(R.id.tv_class_name);
        tvClassName.setText(item.name);

        //包含单元名称的整体相对布局
        final RelativeLayout rlGroup = helper.getView(R.id.rl_group);

        final ImageView iv_arrow = helper.getView(R.id.iv_arrow);

        //缴费比列
        TextView tvJfmx = helper.getView(R.id.tv_jfmx);
        tvJfmx.setText(item.payyes + "/" + (item.payyes + item.payno));

        //缴费明细按钮
        Button btJfmx = helper.getView(R.id.bt_jfmx);
        btJfmx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到缴费明细界面
//                Intent intent = new Intent(context, MasterPaystateDetailsActivity.class);
//                intent.putExtra("c", c);
//                intent.putExtra("classnum", (helper.getLayoutPosition() + 1) + "");
//                context.startActivity(intent);
            }
        });


        //学生出勤率
        TextView tvxscql = helper.getView(R.id.tv_xscql);
        NumberFormat nt = NumberFormat.getPercentInstance();
        //设置百分数精确度0即保留0位小数
        nt.setMinimumFractionDigits(1);
        tvxscql.setText(nt.format(item.averageAttendance));


        //学生平均成绩
        TextView tvxspjcj = helper.getView(R.id.tv_xspjcj);
        //设置平均成绩
        tvxspjcj.setText(MathUtil.one(item.averageExam) + "");

        //备课数
        TextView tvBks = helper.getView(R.id.tv_bks);
        tvBks.setText(item.averageSelef + "");

        if (helper.getLayoutPosition() == 0) {
            rlGroup.setBackgroundColor(context.getResources().getColor(R.color.opencolor));
//            tvClassName.setTextColor(context.getResources().getColor(R.color.white));
            iv_arrow.setImageResource(R.drawable.up_arrow);
        } else {
            rlGroup.setBackgroundColor(context.getResources().getColor(R.color.white));
//            tvClassName.setTextColor(context.getResources().getColor(R.color.base_msg_bg));
            iv_arrow.setImageResource(R.drawable.down_arrow);
        }


        //包含切换箭头的整体布局
        final RelativeLayout buttonLayout = helper.getView(R.id.button);
        expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
            @Override
            public void onPreOpen() {
//                createRotateAnimator(buttonLayout, 0f, 180f).start();
                expandState.put(helper.getLayoutPosition(), true);

                //设置背景和字体颜色
                rlGroup.setBackgroundColor(context.getResources().getColor(R.color.opencolor));
//                tvClassName.setTextColor(context.getResources().getColor(R.color.white));
                iv_arrow.setImageResource(R.drawable.up_arrow);
            }

            @Override
            public void onPreClose() {
//                createRotateAnimator(buttonLayout, 180f, 0f).start();
                expandState.put(helper.getLayoutPosition(), false);
                rlGroup.setBackgroundColor(context.getResources().getColor(R.color.white));
//                tvClassName.setTextColor(context.getResources().getColor(R.color.base_msg_bg));
                iv_arrow.setImageResource(R.drawable.down_arrow);
            }
        });

//        buttonLayout.setRotation(expandState.get(helper.getLayoutPosition()) ? 180f : 0f);

        //包含单元名称的整体相对布局

        rlGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                onClickButton(expandableLayout);
            }
        });

    }


    /**
     * 创建动画的方法
     *
     * @param target
     * @param from
     * @param to
     * @return
     */
    public ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
        animator.setDuration(300);
        animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
        return animator;
    }

    /**
     * 折叠布局的切换方法
     *
     * @param expandableLayout
     */
    private void onClickButton(final ExpandableLayout expandableLayout) {
        expandableLayout.toggle();
    }
}
